/*
 * @Author: zhouwei wei.zhou@flip.fun
 * @Date: 2024-04-08 20:20:31
 * @LastEditors: zhouwei wei.zhou@flip.fun
 * @LastEditTime: 2024-04-08 20:26:31
 * @FilePath: /50/手撕系列/xhr.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
function request() {
	return new Promise(function (resolve, reject) {
		const xhr = new XMLHttpRequest()
		xhr.open('GET', 'http://localhost', true)
		xhr.onreadystatechange = function (e) {
			if (this.readyState !== 4) return
			if (this.status === 200) {
				resolve(this.response)
			} else {
				reject(new Error(this.responseText))
			}
		}
		xhr.setRequestHeader('Accept', 'application/json')
		xhr.responseType = 'json'
		xhr.send(null)
		xhr.onerror = function (e) {
			reject(new Error(this.statusText))
		}
	})
}
